package cn.codecrab.ai.langchain4j.controller;

import cn.codecrab.ai.langchain4j.assistant.QwenAssistant;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author 王刚
 * @since 2025年05月10日 10:48
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Resource
    private OpenAiChatModel openAiChatModel;

    @Resource
    private OllamaChatModel ollamaChatModel;

    @Resource
    private QwenChatModel qwenChatModel;

    @Resource
    private QwenAssistant qwenAssistant;

    @GetMapping("/deepseek")
    public String deepseekChat(@RequestParam("question") String question) {
        return openAiChatModel.chat(question);
    }

    @GetMapping("/ollama")
    public String ollamaChat(@RequestParam("question") String question) {
        return ollamaChatModel.chat(question);
    }

    @GetMapping("/qwen")
    public String qwenChat(@RequestParam("question") String question) {
        return qwenChatModel.chat(question);
    }

    @GetMapping("/wanx")
    public String wanxChat(@RequestParam("question") String question) {
        WanxImageModel wanxImageModel = WanxImageModel.builder()
            .modelName("wanx2.1-t2i-plus")
            .apiKey(System.getenv("DASH_SCOPE_API_KEY"))
            .build();
        Response<Image> response = wanxImageModel.generate(question);
        return response.content().url().toString();
    }

    @GetMapping("/assistant")
    public String assistantChat(@RequestParam("question") String question) {
        return qwenAssistant.chat(question);
    }

    @GetMapping("/v")
    public String v(@RequestParam("question") String question) {
        return qwenAssistant.chat("v-1186fd86-23f5-4df6-8326-ba9c2e7b8e89", question, 21);
    }

    @GetMapping("/prompt")
    public String prompt(@RequestParam("question") String question, @RequestParam("prompt") String prompt) {
        return qwenAssistant.chat("v-1", question, prompt);
    }

    @GetMapping("/memory")
    public void memoryChat() {
        System.out.println(qwenAssistant.chat("a", "我是王刚"));
        System.out.println(qwenAssistant.chat("a", "我是谁"));

        System.out.println(qwenAssistant.chat("b", "我是谁"));
        System.out.println();
    }

    @GetMapping("/calculator")
    public void calculatorChat() {
        System.out.println(qwenAssistant.chat(UUID.randomUUID().toString(), "帮我计算215132+411052，45120-5412.2，68541*45.55，14551/45.1，1561231564的平方根"));
    }

}
